package com.example.kyyl.http;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public abstract class CallBackUtil<T> {
    public static Handler mMainHandler = new Handler(Looper.getMainLooper());

    public static abstract class CallBackDefault extends CallBackUtil<Response> {
        public Response onParseResponse(Call call, Response response) {
            return response;
        }
    }

    public abstract void onFailure(Call call, Exception exc);

    public abstract T onParseResponse(Call call, Response response);

    public void onProgress(float f, long j) {
    }

    public abstract void onResponse(T t);

    public void onError(final Call call, final Exception exc) {
        mMainHandler.post(new Runnable() {
            public void run() {
                CallBackUtil.this.onFailure(call, exc);
            }
        });
    }

    public void onSeccess(Call call, Response response) {
        final Object onParseResponse = onParseResponse(call, response);
        mMainHandler.post(new Runnable() {
            public void run() {
                CallBackUtil.this.onResponse((T) onParseResponse);
            }
        });
    }

    public static abstract class CallBackString extends CallBackUtil<String> {
        public String onParseResponse(Call call, Response response) {
            try {
                return response.body().string();
            } catch (IOException unused) {
                new RuntimeException("failure");
                return "";
            }
        }
    }

    public static abstract class CallBackBitmap extends CallBackUtil<Bitmap> {
        private int mTargetHeight;
        private int mTargetWidth;

        public CallBackBitmap() {
        }

        public CallBackBitmap(int i, int i2) {
            this.mTargetWidth = i;
            this.mTargetHeight = i2;
        }

        public CallBackBitmap(ImageView imageView) {
            int width = imageView.getWidth();
            int height = imageView.getHeight();
            if (width <= 0 || height <= 0) {
                throw new RuntimeException("无法获取ImageView的width或height");
            }
            this.mTargetWidth = width;
            this.mTargetHeight = height;
        }

        public Bitmap onParseResponse(Call call, Response response) {
            if (this.mTargetWidth == 0 || this.mTargetHeight == 0) {
                return BitmapFactory.decodeStream(response.body().byteStream());
            }
            return getZoomBitmap(response);
        }

        private Bitmap getZoomBitmap(Response response) {
            byte[] bArr;
            try {
                bArr = response.body().bytes();
            } catch (IOException e) {
                e.printStackTrace();
                bArr = null;
            }
            BitmapFactory.Options options = new BitmapFactory.Options();
            int i = 1;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
            int i2 = options.outWidth;
            int i3 = options.outHeight;
            int floor = (int) Math.floor((double) (((float) i2) / ((float) this.mTargetWidth)));
            int floor2 = (int) Math.floor((double) (((float) i3) / ((float) this.mTargetHeight)));
            if (floor > 1 || floor2 > 1) {
                i = Math.max(floor, floor2);
            }
            options.inSampleSize = i;
            options.inJustDecodeBounds = false;
            Bitmap decodeByteArray = BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
            if (decodeByteArray != null) {
                return decodeByteArray;
            }
            throw new RuntimeException("Failed to decode stream.");
        }
    }

    public static abstract class CallBackFile extends CallBackUtil<File> {
        private final String mDestFileDir;
        private final String mdestFileName;

        public CallBackFile(String str, String str2) {
            this.mDestFileDir = str;
            this.mdestFileName = str2;
        }

        /* JADX WARNING: Removed duplicated region for block: B:42:0x008e A[Catch:{ IOException -> 0x0091 }] */
        /* JADX WARNING: Removed duplicated region for block: B:45:0x0093 A[SYNTHETIC, Splitter:B:45:0x0093] */
        /* JADX WARNING: Removed duplicated region for block: B:53:0x00a4 A[Catch:{ IOException -> 0x00a7 }] */
        /* JADX WARNING: Removed duplicated region for block: B:56:0x00a9 A[SYNTHETIC, Splitter:B:56:0x00a9] */
        /* JADX WARNING: Removed duplicated region for block: B:63:? A[RETURN, SYNTHETIC] */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public File onParseResponse(okhttp3.Call r19, okhttp3.Response r20) {
            /*
                r18 = this;
                r7 = r18
                r1 = 8192(0x2000, float:1.14794E-41)
                byte[] r8 = new byte[r1]
                okhttp3.ResponseBody r1 = r20.body()     // Catch:{ Exception -> 0x007e, all -> 0x0079 }
                java.io.InputStream r10 = r1.byteStream()     // Catch:{ Exception -> 0x007e, all -> 0x0079 }
                okhttp3.ResponseBody r1 = r20.body()     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                long r11 = r1.contentLength()     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                r1 = 0
                java.io.File r3 = new java.io.File     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                java.lang.String r4 = r7.mDestFileDir     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                r3.<init>(r4)     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                boolean r4 = r3.exists()     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                if (r4 != 0) goto L_0x0028
                r3.mkdirs()     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
            L_0x0028:
                java.io.File r13 = new java.io.File     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                java.lang.String r4 = r7.mdestFileName     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                r13.<init>(r3, r4)     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                java.io.FileOutputStream r14 = new java.io.FileOutputStream     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
                r14.<init>(r13)     // Catch:{ Exception -> 0x0075, all -> 0x0072 }
            L_0x0034:
                int r3 = r10.read(r8)     // Catch:{ Exception -> 0x006e, all -> 0x006b }
                r4 = -1
                if (r3 == r4) goto L_0x0056
                long r4 = (long) r3     // Catch:{ Exception -> 0x006e, all -> 0x006b }
                long r15 = r1 + r4
                r1 = 0
                r14.write(r8, r1, r3)     // Catch:{ Exception -> 0x006e, all -> 0x006b }
                android.os.Handler r5 = mMainHandler     // Catch:{ Exception -> 0x006e, all -> 0x006b }
                com.example.kyyl.http.CallBackUtil$CallBackFile$1 r6 = new com.example.kyyl.http.CallBackUtil$CallBackFile$1     // Catch:{ Exception -> 0x006e, all -> 0x006b }
                r1 = r6
                r2 = r7
                r3 = r15
                r9 = r5
                r7 = r6
                r5 = r11
                r1.<init>(r3, r5)     // Catch:{ Exception -> 0x006e, all -> 0x006b }
                r9.post(r7)     // Catch:{ Exception -> 0x006e, all -> 0x006b }
                r1 = r15
                r7 = r18
                goto L_0x0034
            L_0x0056:
                r14.flush()     // Catch:{ Exception -> 0x006e, all -> 0x006b }
                okhttp3.ResponseBody r1 = r20.body()     // Catch:{ IOException -> 0x0065 }
                r1.close()     // Catch:{ IOException -> 0x0065 }
                if (r10 == 0) goto L_0x0065
                r10.close()     // Catch:{ IOException -> 0x0065 }
            L_0x0065:
                if (r14 == 0) goto L_0x006a
                r14.close()     // Catch:{ IOException -> 0x006a }
            L_0x006a:
                return r13
            L_0x006b:
                r0 = move-exception
                r1 = r0
                goto L_0x009b
            L_0x006e:
                r0 = move-exception
                r1 = r0
                r9 = r10
                goto L_0x0082
            L_0x0072:
                r0 = move-exception
                r1 = r0
                goto L_0x007c
            L_0x0075:
                r0 = move-exception
                r1 = r0
                r9 = r10
                goto L_0x0081
            L_0x0079:
                r0 = move-exception
                r1 = r0
                r10 = 0
            L_0x007c:
                r14 = 0
                goto L_0x009b
            L_0x007e:
                r0 = move-exception
                r1 = r0
                r9 = 0
            L_0x0081:
                r14 = 0
            L_0x0082:
                r1.printStackTrace()     // Catch:{ all -> 0x0098 }
                okhttp3.ResponseBody r1 = r20.body()     // Catch:{ IOException -> 0x0091 }
                r1.close()     // Catch:{ IOException -> 0x0091 }
                if (r9 == 0) goto L_0x0091
                r9.close()     // Catch:{ IOException -> 0x0091 }
            L_0x0091:
                if (r14 == 0) goto L_0x0096
                r14.close()     // Catch:{ IOException -> 0x0096 }
            L_0x0096:
                r1 = 0
                return r1
            L_0x0098:
                r0 = move-exception
                r1 = r0
                r10 = r9
            L_0x009b:
                okhttp3.ResponseBody r2 = r20.body()     // Catch:{ IOException -> 0x00a7 }
                r2.close()     // Catch:{ IOException -> 0x00a7 }
                if (r10 == 0) goto L_0x00a7
                r10.close()     // Catch:{ IOException -> 0x00a7 }
            L_0x00a7:
                if (r14 == 0) goto L_0x00ac
                r14.close()     // Catch:{ IOException -> 0x00ac }
            L_0x00ac:
                throw r1
            */
            throw new UnsupportedOperationException("Method not decompiled: com.example.kyyl.http.CallBackUtil.CallBackFile.onParseResponse(okhttp3.Call, okhttp3.Response):java.io.File");
        }
    }
}
